package com.code.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.code.app.R;

public class IndexBar extends View {

    private String[] alphabet = {
            "A", "B", "C", "D", "E", "F",
            "G", "H", "I", "J", "K", "L",
            "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X",
            "Y", "Z", "#"
    };

    private String touchedABC;

    private Paint defaulePaint;

    private Paint selPaint;


    public IndexBar(Context context) {
        super(context);
    }

    public IndexBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        defaulePaint = new Paint();
        defaulePaint.setAntiAlias(true);
        defaulePaint.setColor(Color.BLACK);
        defaulePaint.setTextSize(sp2px(15));

        selPaint = new Paint();
        selPaint.setAntiAlias(true);
        selPaint.setColor(Color.BLUE);
        selPaint.setTextSize(sp2px(15));
    }

    private float sp2px(int sp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //计算单个文字的宽
        Rect rect = new Rect();
        defaulePaint.getTextBounds("A", 0, 1, rect);
        int textWidth = rect.width();

        int width = textWidth + getPaddingLeft() + getPaddingRight();
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int itemHeight = (getHeight() - getPaddingBottom() - getPaddingTop()) / alphabet.length;
        for (int i = 0; i < alphabet.length; i++) {
            Rect rect = new Rect();
            defaulePaint.getTextBounds(alphabet[i], 0, 1, rect);
            int textWidth = rect.width();
            int dx = getWidth() / 2 - textWidth / 2;

            Paint.FontMetricsInt fontMetricsInt = defaulePaint.getFontMetricsInt();
            int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
            int baseLine = getPaddingTop() + i * itemHeight + itemHeight / 2 + dy;
            if (alphabet[i].equals(touchedABC)) {
                canvas.drawText(alphabet[i], dx, baseLine, selPaint);
            } else {
                canvas.drawText(alphabet[i], dx, baseLine, defaulePaint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_DOWN:
                float height = event.getY();
                int itemHeight = (getHeight() - getPaddingBottom() - getPaddingTop()) / alphabet.length;
                int touchedIndex = (int) ((height - getPaddingTop()) / itemHeight);
                if (touchedIndex < 0) {
                    touchedIndex = 0;
                }
                if (touchedIndex > alphabet.length-1) {
                    touchedIndex = alphabet.length - 1;
                }
                if (!alphabet[touchedIndex].equals(touchedABC)) {
                    touchedABC = alphabet[touchedIndex];
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                touchedABC = "";
                invalidate();
                break;
        }
        return true;
    }
}
